<template>
  <div class="newsparticulars-contain">
    <div class="text">
      <div class="title">{{ newsContent.title }}</div>
      <div class="explain">
        <span>发布时间：{{ $momentFun(newsContent.add_time) }}</span
        ><span>阅读：{{ newsContent.click }}</span>
      </div>
      <hr />
      <div class="content" v-html="newsContent.content"></div>
    </div>
    <div class="footer">
      <textarea
        placeholder="请友善留言"
        rows="4"
        v-model="commit"
        maxlength="50"
        @input="limit"
      ></textarea>
      <div class="limit">{{ wordsNum }}/50</div>
      <van-button type="primary" @click="publish">评论</van-button>
      <div class="commit" v-for="item in commits" :key="item.id">
        <div>{{ item.user_name }}：{{ item.content }}</div>
        <div>评论时间：{{ $momentFun(item.add_time) }}</div>
      </div>
      <van-button style="color: red; border-color: red" @click="more"
        >查看更多</van-button
      >
    </div>
  </div>
</template>

<script setup>
import { reqgetOneNews, reqgetNewsComments, pubCommit } from "@/api";
import { Toast } from "vant";
import { ref, onMounted } from "vue";
import { useRoute } from "vue-router";
const $route = useRoute();
const pageindex = ref(1);
const newsContent = ref([]);
const commits = ref([]);
const commit = ref("");
const wordsNum = ref(0);

const limit = () => {
  wordsNum.value++;
};

const publish = async () => {
  if (commit.value === "") {
    Toast("评论不能为空");
    return;
  }
  let { message } = await pubCommit($route.params.id, commit.value);
  Toast(message);
  commit.value = "";
  wordsNum.value = 0;
  getNewsComments();
};

const more = async () => {
  pageindex.value++;
  let { message } = await reqgetNewsComments($route.params.id, pageindex.value);
  if (message.length === 0) {
    Toast("没有更多评论");
    return;
  }
  commits.value = [...commits.value, ...message];
};

const getOneNews = async () => {
  let { message } = await reqgetOneNews($route.params.id);
  newsContent.value = message;
};

const getNewsComments = async () => {
  let { message } = await reqgetNewsComments($route.params.id, pageindex.value);
  commits.value = message;
};

onMounted(() => {
  getOneNews();
  getNewsComments();
});
</script>

<style lang="scss" scoped>
.newsparticulars-contain {
  padding: 10px;
  background: #fff;

  .text {
    line-height: 22px;

    .title {
      margin: 22px 0;
      font-size: 22px;
      font-weight: 700;
      line-height: 30px;
    }

    .explain {
      display: flex;
      justify-content: space-between;
      font-size: 12px;
    }

    .content {
      ::v-deep p {
        margin: 16px 0;
        font-size: 16px;
      }
    }
  }

  .footer {
    textarea {
      width: 100%;
      font-size: 14px;
      border-color: transparent;
    }

    .limit {
      display: flex;
      flex-direction: row-reverse;
      font-size: 12px;
    }

    .van-button {
      width: 100%;
      margin-top: 20px;
    }

    .commit {
      padding: 5px 0;
      margin-top: 10px;
      font-size: 12px;
      line-height: 16px;
      background: #f2f2f2;
    }
  }
}
</style>
